import Axios from 'axios'
import Qs from 'qs'
import router from '@/router/index'
import cookie from './cookie'
import { Message } from 'view-design'

var axios = Axios.create({
    // baseURL: http.server.url,
    timeout: '500000000000', //请求超时时间
    //header传值，例如：Authorization
    headers: {
        // browserType: "0"
    }
})
// 请求拦截器
// axios.interceptors.request.use(function (config) {
//   // console.log(config.url.includes('login'));
//   let token = cookie.getCookie('token');
//   if (token && !config.url.includes('login')) {
//     config.headers['Authorization'] = token;
//     config.headers['isBigScreen'] = 2;
//   }
//   return config;
// }, function (error) {
//   return Promise.reject(error)
// });
// 响应拦截器
axios.interceptors.response.use(function (request) {
    switch (request.data.resultCode) {
        case '401':
            Message.error({
                content: '登录超时，请重新登录',
                duration: 4,
            })
            break;
        case 1001:
        // cookie.removeCookie('token');
        // router.push({path: '/'});
    }
    return request
}, function (error) {
    let obj = {}
    obj.error = error
    let status = obj.error.response.status
    // console.log(status);
    if (status) {
        switch (status) {
            case 404:
                Message.error({
                    content: '请求网址错误',
                    duration: 4
                });
                break;
            case 405:
                Message.error({
                    content: '请求方式错误',
                    duration: 3
                });
                break
            case 500:
                return Promise.reject(
                    Message.error({
                        content: "服务器内部错误,请及时联系管理员",
                        duration: 5
                    })
                );
        }
    } else {
        Message.error({
            content: '与服务器连接失败，请迅速联系管理员',
            duration: 5
        });
    }
    //通用异常
    return Promise.reject(error)
})

// Http Util 基础服务
export default {
    fetch(url, params) {
        return new Promise((resolve, reject) => {
            axios.get(url, params).then(res => {
                resolve(res)
            }).catch(error => {
                reject(error)
            })
        })
    },
    axiosPost: function (url, data) {
        if (!data) data = {}
        return new Promise((resolve, reject) => {
            axios.post(url, data).then(response => {
                resolve(response)
            }).catch(error => {
                reject(error)
            })
        })
    },
    // form-data格式化
    postForm: function (url, data) {
        if (!data) data = {}
        return new Promise((resolve, reject) => {
            axios.post(url, data, {
                transformRequest: this.jsonToFormData,
                headers: { 'Content-Type': 'application/x-www-form-urlencoded; charset=utf-8' },
            }).then(response => {
                resolve(response)
            }).catch(error => {
                reject(error)
            })
        })
    },
    jsonToFormData: function (data) {
        data = Qs.stringify(data, { arrayFormat: 'brackets' }) // qs.stringify(params)
        return data
    },
    axiosPostFile: function (url, data) {
        if (!data) data = {}
        return new Promise((resolve, reject) => {
            axios.post(url, data, {
                headers: { 'Content-Type': 'application/json;charset=UTF-8' },
                responseType: 'arraybuffer'
            }).then(response => {
                resolve(response)
            }).catch(error => {
                reject(error)
            })
        })
    },
}
